package com.yys.gulimall.ware.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

import static com.yys.gulimall.ware.constant.WareConstant.*;


/**
 * @author zq
 * @version 1.0
 * @date 2021/4/5 15:20
 */
@Configuration
public class MyMQConfig {
    /**
     * 我们使用如下方式
     * 注意属性发生变化之后不会覆盖,需要删掉重新创建
     * c ---------->   order-event-exchange     -----order.create.key--------->   order-create-queue
     *                       |           ^                                                 |
     *                order.release.key  |                                                 |
     *                       √           |                                                 √
     *        order-release-queue        --------------------------------------------------
     * */
    @Bean
    public Queue stockDelayQueue() {
        /*
            设置属性
            x-dead-letter-exchange: order-event-exchange
            x-dead-letter-routing-key: order.release.binding
            x-message-ttl: 60000

         */
        HashMap<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange",STOCK_EVENT_EXCHANGE);
        args.put("x-dead-letter-routing-key",STOCK_RELEASE_ROUTING_KEY);
        args.put("x-message-ttl",ORDER_TTL);

        return new Queue(
                STOCK_DELAY_QUEUE,
                true,
                false,
                false,
                args
        );
    }

    @Bean
    public Queue stockReleaseQueue() {
        return new Queue(
                STOCK_RELEASE_QUEUE,
                true,
                false,
                false
        );
    }

    @Bean
    public Exchange stockEventExchange() {
        return new TopicExchange(
                STOCK_EVENT_EXCHANGE,
                true,
                false
        );
    }

    @Bean
    public Binding stockLockedBinding() {
        return new Binding(
                STOCK_DELAY_QUEUE,
                Binding.DestinationType.QUEUE,
                STOCK_EVENT_EXCHANGE,
                STOCK_LOCKED_ROUTING_KEY,
                null
        );
    }

    @Bean
    public Binding stockReleaseBinding() {
        return new Binding(
                STOCK_RELEASE_QUEUE,
                Binding.DestinationType.QUEUE,
                STOCK_EVENT_EXCHANGE,
                STOCK_RELEASE_BINDING_ROUTING_KEY,
                null
        );
    }



}
